<template>
  <div class="news-list">
    <h1 class="title">新闻列表</h1>

    <el-row :gutter="24" class="news-grid">
      <el-col
          v-for="item in pagedNews"
          :key="item.id"
          :xs="24"
          :sm="24"
          :md="12"
          :lg="12"
      >
        <el-card
            shadow="hover"
            class="news-card"
            @click="goDetail(item.id)"
        >
          <div class="card-content">
            <div class="image-wrapper" v-if="item.coverImage">
              <img
                  :src="item.coverImage"
                  alt="封面图"
                  class="cover-image"
              />
            </div>
            <div class="news-info">
              <h3 class="news-title">{{ item.title }}</h3>
              <p class="news-meta">{{ item.publishTime }}</p>
              <p class="news-summary">
                {{ truncateText(stripHtml(item.content), 60) }}
              </p>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <div class="pagination">
      <el-pagination
          background
          layout="prev, pager, next"
          :total="news.length"
          :page-size="pageSize"
          v-model:current-page="currentPage"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue';
import { useRouter } from 'vue-router';
import { listNewsInfo } from '@/api/news'; // 这里引入你提供的接口方法
import { stripHtml, truncateText } from '@/utils/text-utils.js'

const router = useRouter();

const news = ref([]);
const currentPage = ref(1);
const pageSize = 4; // 每页显示条数

// 分页计算
const pagedNews = computed(() => {
  const start = (currentPage.value - 1) * pageSize;
  return news.value.slice(start, start + pageSize);
});

// 获取新闻数据
const fetchNews = async () => {
  try {
    const res = await listNewsInfo({ page: currentPage.value, size: pageSize });
    // 如果后端返回的是分页数据结构 { records: [], total: xxx }
    news.value = res.data.rows;
  } catch (err) {
    console.error('获取新闻列表失败', err);
  }
};

// 监听分页变化自动加载数据（如果接口支持分页参数）
watch(currentPage, () => {
  fetchNews();
});

// 初始化加载
onMounted(() => {
  fetchNews();
});

// 跳转详情
const goDetail = (id) => {
  router.push(`/news/${id}`);
};
</script>

<style scoped>
.news-list {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.title {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 24px;
  text-align: center;
}

.news-grid {
  margin-bottom: 20px;
}

.news-card {
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  margin-bottom: 24px;
  border-radius: 8px;
  overflow: hidden;
}

.news-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
}

.card-content {
  display: flex;
  flex-direction: column;
}

.image-wrapper {
  position: relative;
  width: 100%;
  padding-top: 56.25%; /* 16:9 比例 */
  overflow: hidden;
}

.cover-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.news-info {
  padding: 16px;
}

.news-title {
  margin: 0 0 8px;
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1.4;
}

.news-meta {
  font-size: 0.85rem;
  color: #999;
  margin-bottom: 10px;
}

.news-summary {
  color: #555;
  font-size: 0.95rem;
  line-height: 1.5;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

/* ===== 移动端优化 ===== */
@media (max-width: 768px) {
  .news-title {
    font-size: 1rem;
  }
  .news-summary {
    font-size: 0.9rem;
  }
}

@media (max-width: 480px) {
  .title {
    font-size: 1.5rem;
  }
  .news-title {
    font-size: 0.95rem;
  }
  .news-meta {
    font-size: 0.8rem;
  }
  .news-summary {
    font-size: 0.85rem;
  }
}
</style>
